package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.AskForLeaveBean;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.mvp.presenter.AskForLeavePresenter;
import com.android.infantschool.model.mvp.presenter.AskForLeavePresenterImpl;
import com.android.infantschool.model.mvp.view.AskForLeaveView;
import com.android.infantschool.ui.adapter.AskForLeaveAdapter;
import com.android.infantschool.util.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AskForLeaveActivity extends BaseActivity implements AskForLeaveAdapter.OnLister, OnRefreshListener, OnLoadMoreListener, AskForLeaveView {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.img_title)
    public ImageView imgTitle;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.recyclerView)
    public RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    public SmartRefreshLayout refreshLayout;

    public AskForLeaveAdapter adapter;
    public AskForLeavePresenter presenter = null;
    private int pageNum = 0;//总页数
    private int pageNow = 1;//当前请求的第几页
    private boolean isRefresh = false;//false:刷新  true:加载

    public static Intent newIntent(Activity activity) {
        Intent intent = new Intent(activity, AskForLeaveActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ask_for_leave);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        presenter = new AskForLeavePresenterImpl(this);
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText("请假调休");
        imgTitle.setVisibility(View.VISIBLE);
        imgTitle.setImageResource(R.mipmap.icon_askfor_leave);

        adapter = new AskForLeaveAdapter(this, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        //设置 Footer 为 球脉冲 样式
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
    }

    private void getList(String page) {
        Map<String, String> map = new HashMap<>();
        map.put("token", AppConfig.getToken());
        map.put("page", page);
        map.put("limit", "5");
        Log.e(TAG, "");
        presenter.postAskForLeave(map);
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.showLoading();
        isRefresh = false;
        pageNow = 1;
        getList(String.valueOf(pageNow));
    }

    @Override
    public void lister(int position) {

    }

    @OnClick({R.id.img_back, R.id.img_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
            case R.id.img_title:
                startActivity(LeaveActivity.newIntent(this));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.onDestroy();
            presenter = null;
        }
    }

    @Override
    public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
        isRefresh = false;
        pageNow = 1;
        getList(String.valueOf(pageNow));
    }

    @Override
    public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
        isRefresh = true;
        pageNow++;
        if (pageNow > pageNum) {//没有最新数据
            refreshLayout.finishLoadMore(true);
            refreshLayout.finishRefresh(true);
            return;
        }
        getList(String.valueOf(pageNow));
    }

    @Override
    public void errorAskForLeave(String error) {
        this.dismissLoading();
        refreshLayout.finishLoadMore(true);
        refreshLayout.finishRefresh(true);
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successAskForLeave(BaseBean baseBean) {
        this.dismissLoading();
        refreshLayout.finishLoadMore(true);
        refreshLayout.finishRefresh(true);
        AskForLeaveBean leaveBean = (AskForLeaveBean) baseBean;
        if (leaveBean.getCode() == 0) {
            pageNum = leaveBean.getCount() / 5 + 1;//总条数
            if (isRefresh) {//加载
                adapter.addDataAll(leaveBean.getData());
            } else {//刷新
                adapter.setData(leaveBean.getData());
            }
            Log.e(TAG, "size:" + adapter.list.size());
        } else {
            ToastUtil.showShortToast(this, "获取失败:" + leaveBean.getMsg());
        }
    }
}